package com.webmall.service;

import com.webmall.dao.BaseDao;
import com.webmall.entity.CartEntity;
import com.webmall.entity.CateEntity;
import com.webmall.entity.ProdEntity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 专门用来处理产品的dao
 */
public class webmall_CartDao {
//    /**
//     * 1.查询所有列表
//     */
//    public static ArrayList<ProdEntity> selectAll(){
//        //声明列表及结果集
//        ArrayList<ProdEntity> list= new ArrayList<ProdEntity>();
//        ResultSet rs = null;//返回的结果集
//        //获取连接对象
//        Connection conn = BaseDao.getConn();
//        PreparedStatement ps = null;
//
//        try {
//            String sql= "SELECT * FROM table_product ORDER BY PRODUCT_ID";
//                ps = conn.prepareStatement(sql);
//                rs=ps.executeQuery();
//
//            while (rs.next()){
//
//                //getString:获取当前列的数据
//                ProdEntity prod = new ProdEntity(
//                        rs.getInt("PRODUCT_ID"),
//                        rs.getString("PRODUCT_NAME"),
//                        rs.getString("PRODUCT_INFO"),
//                        rs.getInt("PRODUCT_PRICE"),
//                        rs.getInt("PRODUCT_STOCK"),
//                        rs.getInt("PRODUCT_FID"),
//                        rs.getInt("PRODUCT_CID"),
//                        rs.getString("PRODUCT_FILENAME")
//
//                );
//                list.add(prod);
//            }
//
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            BaseDao.closeSql(rs,ps,conn);
//        }
//        return list;
//    }

    /**
     * 2.添加一条prod
     * @param
     * @return
     */
    public static int insertCart(CartEntity cart){

        //1。声明sql语句
        //into 表名称，后面是格式化date
        String sql = "INSERT INTO table_cart VALUES(null,?,?,?,?,?,?,?,1) ";

        //2。调动DAO-第二个参数为object数数组
        Object[] params = {
                cart.getCART_P_FILENAME(),
                cart.getCART_P_NAME(),
                cart.getCART_P_PRICE(),
                cart.getCART_MOUNT(),
                cart.getCART_STOCK(),
                cart.getCART_PID(),
                cart.getCART_UID()

        };

        return BaseDao.sqlOperat(sql,params);
    }

    /**
     * 3.根据传入的进行    购物车搜索U
     * @param
     * @return
     */
    public static ArrayList<CartEntity> selectByUId(String uid){

        ResultSet rs = null;
        CateEntity cate = null;
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ArrayList<CartEntity> list= new ArrayList<CartEntity>();


        try {
            String sql ="";
            sql = "SELECT * FROM table_cart WHERE CART_UID=? and CART_VALID=1 ORDER BY CART_ID desc ";


            ps=conn.prepareStatement(sql);
            ps.setString(1,uid);
            rs = ps.executeQuery();

            while (rs.next()){
                CartEntity cart = new CartEntity(
                        rs.getInt("CART_ID"),
                        rs.getString("CART_P_FILENAME"),
                        rs.getString("CART_P_NAME"),
                        rs.getString("CART_P_PRICE"),
                        rs.getInt("CART_MOUNT"),
                        rs.getInt("CART_P_STOCK"),
                        rs.getInt("CART_PID"),
                        rs.getString("CART_UID"),
                        rs.getInt("CART_VALID")

                );
                list.add(cart);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeSql(rs,ps,conn);
        }

        return list;

    }

    public static CartEntity selectById(String uid,String pid){

        ResultSet rs = null;
        CartEntity cart = null;
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;


        try {
            String sql ="";
            sql = "SELECT * FROM table_cart WHERE CART_UID=? and CART_PID=? and CART_VALID=1 ORDER BY CART_ID desc ";


            ps=conn.prepareStatement(sql);
            ps.setString(1,uid);
            ps.setInt(2,Integer.valueOf(pid));
            rs = ps.executeQuery();

            while (rs.next()){
               cart = new CartEntity(
                        rs.getInt("CART_ID"),
                        rs.getString("CART_P_FILENAME"),
                        rs.getString("CART_P_NAME"),
                        rs.getString("CART_P_PRICE"),
                        rs.getInt("CART_MOUNT"),
                        rs.getInt("CART_P_STOCK"),
                        rs.getInt("CART_PID"),
                        rs.getString("CART_UID"),
                        rs.getInt("CART_VALID")

                );

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeSql(rs,ps,conn);
        }

        return cart;

    }

    /**
     * 在购物车中根据cartid获取cart
     * @param
     * @param
     * @return
     */
    public static CartEntity selectByCARTId(String cartId){

        ResultSet rs = null;
        CartEntity cart = null;
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;


        try {
            String sql ="";
            sql = "SELECT * FROM table_cart WHERE CART_ID=?  ";


            ps=conn.prepareStatement(sql);
            ps.setInt(1,Integer.valueOf(cartId));
            rs = ps.executeQuery();

            while (rs.next()){
                cart = new CartEntity(
                        rs.getInt("CART_ID"),
                        rs.getString("CART_P_FILENAME"),
                        rs.getString("CART_P_NAME"),
                        rs.getString("CART_P_PRICE"),
                        rs.getInt("CART_MOUNT"),
                        rs.getInt("CART_P_STOCK"),
                        rs.getInt("CART_PID"),
                        rs.getString("CART_UID"),
                        rs.getInt("CART_VALID")

                );

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeSql(rs,ps,conn);
        }

        return cart;

    }
    /**
     * 4.传入cate 进行信息更新
     * @param
     * @return
     */
    public static int updateCart(int id,int mount){
        //1。声明sql语句
        //into 表名称，后面是格式化date
        String sql = "update table_cart set CART_MOUNT=? where CART_ID=?";

        //2。调动DAO-第二个参数为object数数组
        Object[] params = {
                mount,id
        };


        //    return BaseDao.sqlOperat(sql,null);
        return BaseDao.sqlOperat(sql,params);
    }

    /**
     * 5 删除
     * @param id
     * @return
     */
    public static int deleteByID(String id){
        String sql = "delete from table_cart where CART_ID=?";

        Object[] params = {Integer.valueOf(id)};

        return BaseDao.sqlOperat(sql,params);
    }

    /**
     * 6 获得所有的分类信息 进行首页展示
     * flag为father 或者child
     * @return
     */

    public static ArrayList<CateEntity> showCate(String flag){
        //声明列表及结果集
        ArrayList<CateEntity> list= new ArrayList<CateEntity>();
        ResultSet rs = null;//返回的结果集
        //获取连接对象
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;

        try {

            String sql= null;
            if (flag != null && flag.equals("father")){
                sql="SELECT * FROM table_cate WHERE CATE_PARENT_ID=0";
            }else {
                sql="SELECT * FROM table_cate WHERE CATE_PARENT_ID!=0";
            }

            ps = conn.prepareStatement(sql);
            rs=ps.executeQuery();

            while (rs.next()){

                //getString:获取当前列的数据
                CateEntity cate = new CateEntity(
                        rs.getInt("CATE_ID"),
                        rs.getString("CATE_NAME"),
                        rs.getInt("CATE_PARENT_ID")

                );
                list.add(cate);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.closeSql(rs,ps,conn);
        }
        return list;
    }
}

